package com.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.pojo.Comment;
import com.pojo.Result;
import com.service.CommentService;
import com.service.OssService;
import com.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author 李浩然
 * @version 1.0
 * @description: TODO
 * @date 2022/11/19 11:13
 */
@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    Result result = new Result();
    @Autowired
    private CommentService commentService;
    @Autowired
    private OssService ossService;
    @Autowired
    private SysUserService sysUserService;


    @PostMapping("/getImageUrl")
    @ResponseBody
    public R uploadOssFile(MultipartFile file){
        System.out.println("file="+file.getOriginalFilename());
        String url = ossService.uploadFileAvatar(file);
        System.out.println(url);
        return R.ok(url).setCode(200);
    }


    //添加一条评论
    @PostMapping
    public Result addComment(@RequestBody Comment comment ,HttpServletRequest req) {
         comment.setAvatarUrl(sysUserService.findUrlByName(comment.getUsername()));
        comment.setCreatetime(new Date());
        System.out.println("picAddress---------"+comment.getImgUrl());
//        System.out.println(comment);
        boolean b = commentService.addComments(comment);
        if (b) {
            result.setCode(200);
            result.setMsg("添加成功");
        } else {
            result.setCode(500);
            result.setMsg("添加失败");
        }
        return result;
    }
//查询所有评论
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findAll(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        List<Comment> allUserId = commentService.findByPage(currentPage,pageSize);
//        List<Comment> rootComments = allUserId.stream().filter(comment -> comment.getPid() == null).collect(Collectors.toList());
//        for (Comment rootComment : rootComments) {
//            rootComment.setChildren(allUserId.stream().filter(comment -> Objects.equals(rootComment.getCId(), comment.getPid())).collect(Collectors.toList()));
//            System.out.println("2---------"+rootComment);
//        }
        List<Comment> all = commentService.findAll();
        for (Comment comments : allUserId) {
            comments.setChildren(all.stream().filter(comment -> Objects.equals(comments.getCId(), comment.getPid())).collect(Collectors.toList()));
        }
        PageInfo<Comment> PageInfo = new PageInfo<>(allUserId);
        if (allUserId != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(PageInfo);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
//    删除评论
    @DeleteMapping("/{cId}")
    public Result deleteComment(@PathVariable Integer cId){
        boolean b = commentService.deleteComment(cId);
        if (b) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }
//点赞
    @PutMapping("0/{cId}")
    public  Result addLikeNum(@PathVariable Integer cId){

        boolean b = commentService.addLikeNum(cId);
        if (b) {
            result.setCode(200);
            result.setMsg("点赞成功");
        }
        return result;
    }
//    取消点赞
    @PutMapping("1/{cId}")
    public  Result redLikeNum(@PathVariable Integer cId){
        boolean b = commentService.redLikeNum(cId);
        if (b) {
            result.setCode(200);
            result.setMsg("取消点赞成功");
        }
        return result;
    }
//    在个人中心根据当前登录的username查询关于用户的所有评论
    @GetMapping("/{currentPage}/{pageSize}/{username}")
    public Result findAllByusername(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@PathVariable String username) {
        List<Comment> allUserId = commentService.findByPageByUsername(currentPage,pageSize,username);
//
        List<Comment> all = commentService.findAll();
        for (Comment comments : allUserId) {
            comments.setChildren(all.stream().filter(comment -> Objects.equals(comments.getCId(), comment.getPid())).collect(Collectors.toList()));
        }
        PageInfo<Comment> PageInfo = new PageInfo<>(allUserId);
        if (allUserId != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(PageInfo);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    //    在个人中心根据当前登录的username查询用户评论过的所有信息
    @GetMapping("/1/{currentPage}/{pageSize}/{username}")
    public Result findByPagePidNullByUsername(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@PathVariable String username) {
        List<Comment> allUserId = commentService.findByPagePidNullByUsername(currentPage,pageSize,username);
        List<Comment> all = commentService.findAll();
        for (Comment comments : allUserId) {
            comments.setChildren(all.stream().filter(comment -> Objects.equals(comments.getCId(), comment.getPid())).collect(Collectors.toList()));
        }
        PageInfo<Comment> PageInfo = new PageInfo<>(allUserId);
        if (allUserId != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(PageInfo);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }


}